package com.chinaisafe.user.controller.publicapi;

import com.chinaisafe.base.common.annotation.OperationLog;
import com.chinaisafe.base.common.enums.BusinessTypeEnum;
import com.chinaisafe.base.common.vo.Answer;
import com.chinaisafe.gateway.sdk.annotation.PublicApi;
import com.chinaisafe.user.service.UserLoginService;
import com.chinaisafe.user.vo.request.*;
import com.chinaisafe.user.vo.response.LoginRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * LoginController
 * Description：用户登录
 *
 * @author xuefu.shu
 * @date  2024-07-12 09:55:54
 */
@PublicApi
@RestController
@RequestMapping("/login")
@Tag(name = "登录相关接口,需要加 /public 前缀", description = "登录相关接口，需要加 /public 前缀")
public class LoginController {

    @Resource
    private UserLoginService userLoginService;

    @GetMapping("/captcha")
    @Operation(summary = "获取验证码", description = "获取验证码，返回base64", hidden = true)
    public Answer<String> captcha(String mobile) {
        UserCaptChaReqVO captChaReqVO = new UserCaptChaReqVO();
        captChaReqVO.setMobile(mobile);

        return Answer.successWithData(userLoginService.captcha(captChaReqVO));
    }

    @PostMapping("/sendCode")
    @Operation(summary = "发送验证码", description = "发送验证码")
    public Answer<String> sendVerificationCode(@RequestBody @Validated SendMobileSmsCodeReqVO smsCodeReqVO) {
        return Answer.successWithData(userLoginService.sendVerificationCode(smsCodeReqVO));
    }


    @PostMapping("/mobile")
    @Operation(summary = "手机号登录", description = "手机号登录")
    @OperationLog(title = "用户手机号登录", businessType = BusinessTypeEnum.GRANT)
    public Answer<LoginRespVO> mobileLogin(@RequestBody @Validated LoginMobileReqVO mobileReqVO) {
        return Answer.successWithData(userLoginService.mobileLogin(mobileReqVO));
    }


    @PostMapping("/account")
    @Operation(summary = "账号登录", description = "账号登录")
    @OperationLog(title = "用户账号登录", businessType = BusinessTypeEnum.GRANT)
    public Answer<LoginRespVO> accountLogin(@RequestBody @Validated LoginAccountReqVO accountReqVO) {
        return Answer.successWithData(userLoginService.accountLogin(accountReqVO));
    }

    @GetMapping("/loginOut")
    @Operation(summary = "退出登录", description = "退出登录")
    @OperationLog(title = "用户退出登录", businessType = BusinessTypeEnum.GRANT)
    public Answer<String> loginOut() {
        return Answer.successWithData(userLoginService.loginOut());
    }

}